var express = require('express');
var router = express.Router();
var db = require("../util/dbconfig");  // 引入数据库方法

var bodyparser = require('body-parser');
// 已解析表单提交数据为例 application/x-www-form-urlencoded
// extended: false 值是false时解析值是“String”或“Array” 值是true的时候可以解析任意类型的数据

//注册 
router.get('/register', (req, res) => {
  console.log(req.query);
  let sql1 = `select * from users where tel = '${req.query.tel}'`  // 查找数据表中是否已经存在用户
  // let sql1 = `select * from users where FIND_IN_SET('${req.query.user}', user)`  // 查找数据表中是否已经存在用户
  let sql2 = `INSERT INTO users(tel,paw) VALUES(?,?)`    // 插入语句，将前端传递过来的手机号和密码插入到数据库中
  db.query(sql1,function(err,data){
      if(err){
          res.send({
                  msg:"注册失败",
                  code:500
          });
      }else {
          if(data.length==0){  // data 为查询出来的结果，如果查询的手机号不存在，将会返回一个空数据 所有此时 data[0]==undefined, 执行插入语句操作;
              db.query(sql2,[req.query.tel,req.query.paw],function(err,data){
                  if(err){
                      res.send({
                          code:500,
                          msg:"注册失败!",
                      });
                      console.log(err)
                  }else {
                      // console.log(data);
                      // res.redirect("/users"); 重定向，添加完后返回到用户首页
                      res.send({
                              msg:"注册成功",
                              code:200
                      });
                  }
              });
          }else{
              // 当tel用户存在时
              res.send({code:0, msg:'用户名已存在'})
          }
      }
  });
});



// 登录
router.get('/signIn', (req, res)=>{
  let {
  tel,
  paw
  } = req.query;

  console.log(req.query)

let sql = `select * from users where FIND_IN_SET('${tel}', tel)`
db.query(sql,(err,data) => {
  // console.log(data)
  if (err) {
      console.log(err);
      return res.json({
          code: -1,
          msg: '登录失败'
      })
  } else {
      console.log("c")
      console.log(data);
      // 当data数组不为空时，代表该手机号注册过，然后匹对密码
      if(data.length==0){
          // 当 data 为空数组时，代表该手机号没有注册
          res.send({
              data:data,
              code:0,
              msg:"该用户没有注册"
          });
      }else{
          if(data[0].paw == paw){
              res.send({
                  data:data[0],
                  code:200,
                  token:"jk1235468956dasldsa",
                  msg:"登录成功！"
              });
          }else{
              res.send({
                  code:500,
                  msg:"密码错误"
              });
          }
      }
  }
  })
});

// 查看是否已经收藏    
router.get('/isStar', (req, res) => {    
    let sql = `select * from star where tel = '${req.query.tel}' && chongwu_id = '${req.query.chongwu_id}'`  // 查找数据表中是否存在
    db.query(sql, (err, data) => {
        if (err) {
            res.send({
                code: 500,
                msg: "获取失败"
            })
        } else {
            res.send({
                list: data,
                code: 200,
                 msg: "获取数据成功"
            })
        }
    });
  });

  // delStar 取消收藏
  router.get('/delStar', (req, res) => {
    let sql = `delete from star where chongwu_id = ${req.query.chongwu_id} && tel = ${req.query.tel}`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({
                code: 500,
                msg: "删除失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "删除成功"
        })
    }
})
})


// 添加到收藏
router.get('/addSart', (req, res) => {
    console.log(req.query);   
    let sql2 = `INSERT INTO star(num,chongwu_id,title,img,tel,xq) VALUES(?,?,?,?,?,?)` 
                db.query(sql2,[req.query.num,req.query.chongwu_id,req.query.title,req.query.img,req.query.tel,req.query.xq],function(err,data){
                    if(err){
                        res.send({
                            code:500,
                            msg:"添加失败!",
                        });
                        console.log(err)
                    }else {
                        res.send({
                                msg:"添加成功!",
                                code:200
                        });
                    }
                });
  });


  // 我的收藏
  router.get('/getMyStar', (req, res) => {
    console.log(req.query);
    let sql1 = `select * from star where tel = '${req.query.tel}'`
    db.query(sql1,function(err,data){
        if(err){
            res.send({
                    msg:"获取失败",
                    code:500
            });
        }else {
            res.send({
                list:data,
                msg:"获取成功！",
                code:"200"
            });
        }
    });
  });



// 添加宠物
router.get('/addData', (req, res) => {
    console.log(req.query);
    let sql = `INSERT INTO chongwu(title,img,num,xq) VALUES(?,?,?,?)`    // 插入语句，将前端传递过来的数据插入到数据库中
        db.query(sql,[req.query.title,req.query.img,req.query.num,req.query.xq],function(err,data){
                if(err){
                    res.send({
                        code:500,
                        msg:"添加失败!",
                    });
                        console.log(err)
                    }else {
                        res.send({
                            msg:"添加成功!",
                             code:200
                    });
                }
        });
  });


  //查询所有宠物
router.get('/getData', (req, res) => {    
    var sql = "select * from chongwu";
    db.query(sql, (err, data) => {
        if (err) {
            res.send({
                code: 500,
                msg: "获取失败"
            })
        } else {
            res.send({
                list: data,
                code: 200,
                 msg: "获取数据成功"
            })
        }
    });
  });
  

// 删除宠物
router.get('/delData', (req, res) => {
    let sql = `delete from chongwu where chongwu_id = ${req.query.chongwu_id}`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({
                code: 500,
                msg: "删除失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "删除成功"
        })
    }
})
})


  // 申请
router.get('/yuyuechongwu', (req, res) => {
    console.log(req.query);   
    let sql2 = `INSERT INTO yuyue(num,chongwu_id,title,img,tel,xq) VALUES(?,?,?,?,?,?)` 
                db.query(sql2,[req.query.num,req.query.chongwu_id,req.query.title,req.query.img,req.query.tel,req.query.xq],function(err,data){
                    if(err){
                        res.send({
                            code:500,
                            msg:"添加失败!",
                        });
                        console.log(err)
                    }else {
                        res.send({
                            msg:"添加成功!",
                            code:200
                    });
            }
        });
  });

    //获取申请宠物
    router.get('/getMyYuyue', (req, res) => {
        let sql1 = `select * from yuyue where tel = '${req.query.tel}'`
        db.query(sql1,function(err,data){
            if(err){
                res.send({
                        msg:"查询失败",
                        code:500
                });
            }else {
                res.send({
                    list:data,
                    msg:"查询成功！",
                    code:"200"
                });
            }
        });
      });

// 查看宠物是否已经申请
router.get('/isShengqing', (req, res) => {    
    let sql = `select * from yuyue where tel = '${req.query.tel}' && chongwu_id = '${req.query.chongwu_id}'`  // 查找数据表中是否存在
    db.query(sql, (err, data) => {
        if (err) {
            res.send({
                code: 500,
                msg: "获取失败"
            })
        } else {
            res.send({
                list: data,
                code: 200,
                 msg: "获取数据成功"
            })
        }
    });
  });


 // 取消申请
 router.get('/delYuyue', (req, res) => {
    let sql = `delete from yuyue where yuyue_id = ${req.query.yuyue_id}`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({
                code: 500,
                msg: "删除失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "删除成功"
        })
    }
})
})





// 用户管理


// 获取所有用户
  router.get('/getUser', (req, res) => {
    let sql1 = `select * from users`
    db.query(sql1,function(err,data){
        if(err){
            res.send({
                    msg:"查询失败",
                    code:500
            });
        }else {
            res.send({
                list:data,
                msg:"查询成功！",
                code:"200"
            });
        }
    });
  });

 // 修改用户信息
 // 修改列表,根据id修改
 router.get('/updateUser', (req,res)=>{
    var sql = `update users set paw='${req.query.paw}',name='${req.query.name}',sex='${req.query.sex}' where id='${req.query.id}'`;
    console.log(req);
    db.query(sql,function(err,data){
        if(err){
            console.log(err,"v")
            res.send("修改失败 " + err);
        }else {
            // res.redirect("/users");
            console.log(data,"a")
            res.send({
               msg:"修改成功",
               code:200
           
            });
        }
    })
})

// 修改密码
 router.get('/updatePost', (req,res)=>{
    var sql = `update users set paw='${req.query.paw}' where id='${req.query.id}'`;
    console.log(req);
    db.query(sql,function(err,data){
        if(err){
            console.log(err,"v")
            res.send("修改失败 " + err);
        }else {
            console.log(data,"a")
            res.send({
               msg:"修改成功",
               code:200
           
            });
        }
    })
})

// 获取用户个人信息
router.get('/getUserInfo', (req, res) => {
    let sql1 = `select * from users where id = '${req.query.id}'`
    db.query(sql1,function(err,data){
        if(err){
            res.send({
                    msg:"查询失败",
                    code:500
            });
        }else {
            res.send({
                list:data,
                msg:"查询成功！",
                code:"200"
            });
        }
    });
  });


// 删除用户
router.get('/delUser', (req, res) => {
    let sql = `delete from users where id = ${req.query.id}`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({
                code: 500,
                msg: "删除失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "删除成功"
        })
    }
})
})

// 修改宠物信息
router.get('/updateData', (req,res)=>{
    var sql = `update chongwu set title='${req.query.title}',xq='${req.query.xq}' where chongwu_id='${req.query.chongwu_id}'`;
    console.log(req);
    db.query(sql,function(err,data){
        if(err){
            res.send("修改失败 " + err);
        }else {
            res.send({
               msg:"修改成功",
               code:200
           
            });
        }
    })
})



// 获取用户申请
  router.get('/getYuyueAll', (req, res) => {
    let sql1 = `select * from yuyue`
    db.query(sql1,function(err,data){
        if(err){
            res.send({
                    msg:"获取失败",
                    code:500
            });
        }else {
            res.send({
                list:data,
                msg:"获取成功！",
                code:"200"
            });
        }
    });
  });


   //  删除收藏
   router.get('/delUserStar', (req, res) => {
    let sql = `delete from star where sc_id = ${req.query.sc_id}`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({
                code: 500,
                msg: "删除失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "删除成功"
        })
    }
})
})

  // 搜索
  router.get('/search', (req, res)=>{
let sql = `select * from chongwu where title REGEXP '${req.query.title}'`
db.query(sql,(err,data) => {
    // console.log(data)
    if (err) {
        console.log(err);
        return res.json({
            code: 500,
            msg: '搜索失败'
        })
    } else {
            console.log(data)
            res.send({
                list:data,
                code:200,
                msg:"搜索成功"
            });
        }
  })
});

  // 用户列表的搜索功能
  router.get('/search1', (req, res)=>{
    let sql = `select * from users where tel REGEXP '${req.query.tel}'`
    db.query(sql,(err,data) => {
        // console.log(data)
        if (err) {
            console.log(err);
            return res.json({
                code: 500,
                msg: '搜索失败'
            })
        } else {
                console.log(data)
                res.send({
                    list:data,
                    code:200,
                    msg:"搜索成功"
                });
            }
      })
    });

 // 申请列表额搜索功能
  router.get('/search2', (req, res)=>{
    let sql = `select * from yuyue where tel REGEXP '${req.query.tel}'`
    db.query(sql,(err,data) => {
        // console.log(data)
        if (err) {
            console.log(err);
            return res.json({
                code: 500,
                msg: '搜索失败'
            })
        } else {
                console.log(data)
                res.send({
                    list:data,
                    code:200,
                    msg:"搜索成功"
                });
            }
      })
    });


// 修改申请状态
router.get('/updateActive', (req,res)=>{
    var sql = `update yuyue set active='${req.query.active}' where yuyue_id='${req.query.yuyue_id}'`;
    console.log(req);
    db.query(sql,function(err,data){
        if(err){
            res.send("修改失败 " + err);
        }else {
            console.log(data,"a")
            res.send({
               msg:"修改成功",
               code:200
           
            });
        }
    })
})

// 发布论坛
router.get('/addLuntan', (req, res) => {
    console.log(req.query);
    let sql = `INSERT INTO luntan(msg,tel,shijian,img,nickName,avatarUrl) VALUES(?,?,?,?,?,?)`   
        db.query(sql,[req.query.msg,req.query.tel,req.query.shijian,req.query.img,req.query.nickName,req.query.avatarUrl],function(err,data){
                if(err){
                    res.send({
                        code:500,
                        msg:"发布失败!",
                    });
                        console.log(err)
                    }else {
                        res.send({
                            msg:"发布成功!",
                             code:200
                    });
                }
        });
  });

  // 删除论坛
  router.get('/delLuntan', (req, res) => {
    let sql = `delete from luntan where luntan_id = ${req.query.luntan_id}`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({
                code: 500,
                msg: "删除失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "删除成功"
        })
    }
 })
})

// 论坛删除后，删除对应的评论信息
router.get('/delPinglun', (req, res) => {
    let sql = `delete from pinglun where luntan_id = ${req.query.luntan_id}`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({
                code: 500,
                msg: "删除失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "删除成功"
        })
    }
})
})

//查询所有的论坛信息
router.get('/getLuntan', (req, res) => {    
    var sql = "select * from luntan";
    db.query(sql, (err, data) => {
        if (err) {
            res.send({
                code: 500,
                msg: "获取失败"
            })
        } else {
            res.send({
                list: data,
                code: 200,
                 msg: "获取数据成功"
            })
        }
    });
  });

// 发表论坛评论
router.get('/addPinglun', (req, res) => {
    console.log(req.query);
    let sql = `INSERT INTO pinglun(content,from_tel,to_tel,luntan_id,from_nickName,to_nickName,shijian,from_img) VALUES(?,?,?,?,?,?,?,?)`   
        db.query(sql,[req.query.content,req.query.from_tel,req.query.to_tel,req.query.luntan_id,req.query.from_nickName,req.query.to_nickName,req.query.shijian,req.query.from_img],function(err,data){
                if(err){
                    res.send({
                        code:500,
                        msg:"发布失败!",
                    });
                        console.log(err)
                    }else {
                        res.send({
                            msg:"发布成功!",
                             code:200
                    });
                }
        });
  });


  // 查看论坛评论数据
router.get('/getLTPinglun', (req, res) => {    
    let sql = `select * from pinglun where luntan_id = '${req.query.luntan_id}'`
    db.query(sql, (err, data) => {
        if (err) {
            res.send({
                code: 500,
                msg: "获取失败"
            })
        } else {
            res.send({
                list: data,
                code: 200,
                 msg: "获取数据成功"
            })
        }
    });
  });
  
  
  // 根据手机号查询用户发布的论坛
  router.get('/searchLuntan', (req, res)=>{
    // 查询语句
    let sql = `select * from luntan where tel REGEXP '${req.query.tel}'`
    db.query(sql,(err,data) => {
        if (err) {
            return res.json({
                code: 500,
                msg: '搜索失败'
            })
        } else {
                res.send({
                    list:data,
                    code:200,
                    msg:"搜索成功"
                });
            }
      })
    });



// 获取所有资讯
router.get('/getZixun', (req, res)=>{
    let sql = `select * from zixun`
    db.query(sql,(err,data) => {
        if (err) {
            console.log(err);
            return res.json({
                code: 500,
                msg: '获取失败'
            })
        } else {
                console.log(data)
                res.send({
                    list:data,
                    code:200,
                    msg:"获取成功"
                });
            }
      })
    });


// 添加资讯
router.get('/addZixun', (req, res) => {
    let sql = `INSERT INTO zixun(title,msg,shijian,img) VALUES(?,?,?,?)`
        db.query(sql,[req.query.title,req.query.msg,req.query.shijian,req.query.img],function(err,data){
                if(err){
                    console.log(err);
                    res.send({
                        code:500,
                        msg:"添加失败!",
                    });
                        console.log(err)
                    }else {
                        res.send({
                            msg:"添加成功!",
                             code:200
                    });
                }
        });
  });

 // 修改资讯
 router.get('/editZixun', (req,res)=>{
    var sql = `update zixun set title='${req.query.title}',msg='${req.query.msg}',shijian='${req.query.shijian}' where zixun_id='${req.query.zixun_id}'`;
    console.log(req.query);
    db.query(sql,function(err,data){
        if(err){
            res.send("修改失败 " + err);
        }else {
            res.send({
               msg:"修改成功",
               code:200
           
            });
        }
    })
})

// 删除资讯
router.get('/delZixun', (req, res) => {
    let sql = `delete from zixun where zixun_id = ${req.query.zixun_id}`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({
                code: 500,
                msg: "删除失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "删除成功"
        })
    }
})
})

 // 根据标题查询资讯
router.get('/search', (req, res)=>{
let sql = `select * from zixun where title REGEXP '${req.query.title}'`
db.query(sql,(err,data) => {
    if (err) {
        console.log(err);
        return res.json({
            code: 500,
            msg: '搜索失败'
        })
    } else {
            console.log(data)
            res.send({
                list:data,
                code:200,
                msg:"搜索成功"
            });
        }
  })
});
   

  

module.exports = router;
